#include "CH9120.h"
#include "stdlib.h"
#include "string.h"
#include "usart.h"
#include "main.h"
#include "gpio.h"

extern unsigned char UART1_Rx_Buf[MAX_REC_LENGTH];
extern unsigned int  UART1_Rx_cnt ;
extern unsigned char UART1_temp[REC_LENGTH];

extern uint8_t CH9120_Dest_IP[4];
extern uint16_t CH9120_Dest_PORT;


uint8_t CH9120_DHCP = CH9120_DHCP_DISABLE; 
uint8_t CH9120_Mode = CH9120_TCP_Client_Mode;  

uint8_t CH9120_Local_IP[4] = {192,168,1,188}; 
uint8_t CH9120_Local_GW[4] = {192,168,1,254};
uint8_t CH9120_Local_MASK[4] = {255,255,255,0};

uint8_t Read_CH9120_Local_IP[4]; 
uint8_t Read_CH9120_Local_GW[4];
uint8_t Read_CH9120_Local_MASK[4];

void CH9120_RESET(void)
{
	RSTI_9120_LOW;
	HAL_Delay(500);
	RSTI_9120_HIGH;
	HAL_Delay(3000);
}

void CH9120_Config_ON(void)
{
	CFG0_9120_LOW;
	HAL_Delay(200); 
}

void CH9120_Config_OFF(void)
{
	CFG0_9120_HIGH;
	HAL_Delay(200); 
}

void CH9120_Set_DHCP(uint8_t val)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_DHCP_cmd[4+1]={0x57,0xab,0x33,0x01};
	UART1_Rx_cnt = 0;
	if(val==CH9120_DHCP_ENABLE) 
	{
		SysPrintf("CH9120 set DHCP begin\r\n");
		HAL_UART_Transmit(&huart1,CH9120_DHCP_cmd,4,1000);
		while(j--)
		{
			HAL_Delay(500);
			SysPrintf("len=%d\n",UART1_Rx_cnt);	
			for(i=0;i<UART1_Rx_cnt;i++)
			{
				
				if(UART1_Rx_Buf[i]==0xaa)
				{
					SysPrintf("CH9120 set DHCP OK\r\n");
					return;
				}
			}
		}
	}
		else
		{
			CH9120_DHCP_cmd[3] = 0x00;
			SysPrintf("CH9120 disable DHCP begin\r\n");
			HAL_UART_Transmit(&huart1,CH9120_DHCP_cmd,4,1000);
			while(j--)
			{
				
				HAL_Delay(500);
				SysPrintf("len=%d\n",UART1_Rx_cnt);	
				for(i=0;i<UART1_Rx_cnt;i++)
				{
					if(UART1_Rx_Buf[i]==0xaa)
					{
						SysPrintf("CH9120 disable DHCP OK\r\n");
						
						return;
					}
				}
			}
			SysPrintf("CH9120 disable DHCP error\r\n");
		}			
		
	SysPrintf("CH9120 set DHCP error\r\n");
}


/*
*********************************************************************************************************
*	º¯ÊýÃû:   CH9120_Set_Local_IP
*	¹¦ÄÜËµÃ÷: set CH9120 local ip
*	·µ»ØÖµ:   null
*   ×÷Õß£º  huangming
*   ÈÕÆÚ£º  2022-05-10
*********************************************************************************************************/
void CH9120_Set_Local_IP(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_LocalIP_cmd[7+1]={0x57,0xab,0x11,CH9120_Local_IP[0],CH9120_Local_IP[1],CH9120_Local_IP[2],CH9120_Local_IP[3]};
	j = 6;
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 set local-ip begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_LocalIP_cmd,7,1000);
	while(j--)
	{
		HAL_Delay(500);
		SysPrintf("len=%d\n",UART1_Rx_cnt);	
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 set local-ip OK\r\n");
				return;
			}
		}
	}
		
	SysPrintf("CH9120 set dest-ip error\r\n");
}

/*
*********************************************************************************************************
*	º¯ÊýÃû:   CH9120_Set_Local_MASK
*	¹¦ÄÜËµÃ÷: set CH9120 local mask
*	·µ»ØÖµ:   null
*   ×÷Õß£º  huangming
*   ÈÕÆÚ£º  2022-05-10
*********************************************************************************************************/
void CH9120_Set_Local_MASK(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_LocalMASK_cmd[7+1]={0x57,0xab,0x12,CH9120_Local_MASK[0],CH9120_Local_MASK[1],CH9120_Local_MASK[2],CH9120_Local_MASK[3]};
	j = 6;
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 set local-mask begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_LocalMASK_cmd,7,1000);
	
	while(j--)
	{
		HAL_Delay(500);
		SysPrintf("len=%d\n",UART1_Rx_cnt);	
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 set local-mask OK\r\n");
				return;
			}
		}
	}
		
	SysPrintf("CH9120 set dest-mask error\r\n");
}

/*
*********************************************************************************************************
*	º¯ÊýÃû:   CH9120_Set_Local_GW
*	¹¦ÄÜËµÃ÷: set CH9120 local gateway
*	·µ»ØÖµ:   null
*   ×÷Õß£º  huangming
*   ÈÕÆÚ£º  2022-05-10
*********************************************************************************************************/
void CH9120_Set_Local_GW(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_LocalGW_cmd[7+1]={0x57,0xab,0x13,CH9120_Local_GW[0],CH9120_Local_GW[1],CH9120_Local_GW[2],CH9120_Local_GW[3]};
	j = 6;
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 set local-gw begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_LocalGW_cmd,7,1000);
	while(j--)
	{
		HAL_Delay(500);
		SysPrintf("len=%d\n",UART1_Rx_cnt);	
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 set local-gw OK\r\n");
				return;
			}
		}
	}
		
	SysPrintf("CH9120 set dest-gw error\r\n");
}

void CH9120_Link_ClearData(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_Link_Clear_cmd[4+1]={0x57,0xab,0x26,0x01};
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 link clear data begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_Link_Clear_cmd,4,1000);
	while(j--)
	{
		HAL_Delay(500);
		SysPrintf("len=%d\n",UART1_Rx_cnt);	
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 link clear data OK\r\n");
				return;
			}
		}
	}
	SysPrintf("CH9120 link clear data error\r\n");
}

void CH9120_Set_Mode(uint8_t val)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_Mode_cmd[4+1]={0x57,0xab,0x10,val};
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 set mode %d begin\r\n",val);
	HAL_UART_Transmit(&huart1,CH9120_Mode_cmd,4,1000);
	while(j--)
	{
		HAL_Delay(500);
		SysPrintf("len=%d\n",UART1_Rx_cnt);	
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 set mode %d OK\r\n",val);
				return;
			}
		}
	}
	SysPrintf("CH9120 set mode %d error\r\n",val);
}

void CH9120_Set_Dest_IP(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_DestIP_cmd[7+1]={0x57,0xab,0x15,CH9120_Dest_IP[0],CH9120_Dest_IP[1],CH9120_Dest_IP[2],CH9120_Dest_IP[3]};
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 set dest-ip begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_DestIP_cmd,7,1000);
	while(j--)
	{
		HAL_Delay(500);
		SysPrintf("len=%d\n",UART1_Rx_cnt);	
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 set dest-ip OK\r\n");
				return;
			}
		}
	}
	SysPrintf("CH9120 set dest-ip error\r\n");
}

void CH9120_Set_Dest_PORT(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_DestPORT_cmd[5+1]={0x57,0xab,0x16,0,0};
	CH9120_DestPORT_cmd[3] = CH9120_Dest_PORT;
	CH9120_DestPORT_cmd[4] = (uint8_t)(CH9120_Dest_PORT>>8);
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 set dest-port begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_DestPORT_cmd,5,1000);
	while(j--)
	{
		HAL_Delay(500);	
		SysPrintf("len=%d\n",UART1_Rx_cnt);
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 set dest-port OK\r\n");
				return;
			}
		}
	}
	SysPrintf("CH9120 set dest-port error\r\n");
}

void CH9120_Updata_EEPROM(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_Updata_EEprom_cmd[3+1]={0x57,0xab,0x0d};
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 updata to eeprom begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_Updata_EEprom_cmd,3,1000);
	while(j--)
	{
		HAL_Delay(500);	
		SysPrintf("len=%d\n",UART1_Rx_cnt);
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 updata to eeprom OK\r\n");
				return;
			}
		}
	}
	SysPrintf("CH9120 updata to eeprom error\r\n");
}


void CH9120_Active_Config(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_Active_Config_cmd[3+1]={0x57,0xab,0x0e};
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 active config begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_Active_Config_cmd,3,1000);
	while(j--)
	{
		HAL_Delay(500);	
		SysPrintf("len=%d\n",UART1_Rx_cnt);
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			if(UART1_Rx_Buf[i]==0xaa)
			{
				SysPrintf("CH9120 active config OK\r\n");
				return;
			}
		}
	}
	SysPrintf("CH9120 active config error\r\n");
}


void CH9120_SentData(uint8_t *buf,uint16_t len)
{
	SysPrintf("Eth0 sent data...\r\n");
	HAL_UART_Transmit(&huart1,buf,len,1000);
}

uint8_t CH9120_Check_Link_Status(void)
{
	uint8_t i;
	uint8_t j = 6;
	CH9120_Config_ON();
	uint8_t CH9120_Check_Link_Status_cmd[3+1]={0x57,0xab,0x03};
	UART1_Rx_cnt = 0;
	SysPrintf("CH9120 Check Link begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_Check_Link_Status_cmd,3,1000);
	while(j--)
	{
		HAL_Delay(500);	
		SysPrintf("len=%d\n",UART1_Rx_cnt);
		for(i=0;i<UART1_Rx_cnt;i++)
		{
			if(UART1_Rx_Buf[i]==0x01)
			{
				SysPrintf("CH9120 Check Link OK\r\n");
				CH9120_Config_OFF();
				return 1;
			}
		}
	}
	SysPrintf("CH9120 Check Link error\r\n");
	CH9120_Config_OFF();
	return 0;
}

void CH9120_Read_Config(void)
{
	uint8_t i;
	
	uint8_t CH9120_Read_DestIP_cmd[3+1]={0x57,0xab,0x65};
	uint8_t CH9120_Read_DestPort_cmd[3+1]={0x57,0xab,0x66};
	uint8_t CH9120_Read_Baund_cmd[3+1]={0x57,0xab,0x71};
	uint8_t CH9120_Read_sourceIP_cmd[3+1]={0x57,0xab,0x61};
	uint8_t CH9120_Read_Mode_cmd[3+1]={0x57,0xab,0x60};
	UART1_Rx_cnt = 0;
	CH9120_RESET();
	




	
	CH9120_Config_ON();
	HAL_UART_Transmit(&huart1,CH9120_Read_DestIP_cmd,3,1000);
	SysPrintf("CH9120 dest ip: ");
	HAL_Delay(1000); 
	
	for(i=0;i<UART1_Rx_cnt;i++)
	{
		SysPrintf("%x",UART1_Rx_Buf[i]);
	}
	SysPrintf("\r\n");
/* 	read_destip3 = (UART1_Rx_Buf[UART1_Rx_cnt-2]<<4) + UART1_Rx_Buf[UART1_Rx_cnt-1];
	read_destip2 = (UART1_Rx_Buf[UART1_Rx_cnt-4]<<4) + UART1_Rx_Buf[UART1_Rx_cnt-3];
	read_destip1 = (UART1_Rx_Buf[UART1_Rx_cnt-6]<<4) + UART1_Rx_Buf[UART1_Rx_cnt-5];
	read_destip0 = (UART1_Rx_Buf[UART1_Rx_cnt-8]<<4) + UART1_Rx_Buf[UART1_Rx_cnt-7];
	SysPrintf("%x,%x,%x,%x\r\n",read_destip0,read_destip1,read_destip2,read_destip3); */
	
	if( (UART1_Rx_Buf[UART1_Rx_cnt-1]== CH9120_Dest_IP[3]) && (UART1_Rx_Buf[UART1_Rx_cnt-2]== CH9120_Dest_IP[2]) && (UART1_Rx_Buf[UART1_Rx_cnt-3]== CH9120_Dest_IP[1]) &&(UART1_Rx_Buf[UART1_Rx_cnt-4]== CH9120_Dest_IP[0]) )
		{
			SysPrintf("CH9120 dest ip correct\r\n");
			SysPrintf("eth0 dest ip:%d.%d.%d.%d\r\n",UART1_Rx_Buf[UART1_Rx_cnt-4],UART1_Rx_Buf[UART1_Rx_cnt-3],UART1_Rx_Buf[UART1_Rx_cnt-2],UART1_Rx_Buf[UART1_Rx_cnt-1]);
		}
		else
		{
			SysPrintf("CH9120 dest ip err and re-config\r\n");
			CH9120_Init(CH9120_Mode);
			
		}
		
	
	
	UART1_Rx_cnt = 0;
	HAL_UART_Transmit(&huart1,CH9120_Read_DestPort_cmd,3,1000);
	SysPrintf("CH9120 dest port: ");
	HAL_Delay(1000); 
	for(i=0;i<UART1_Rx_cnt;i++)
	{
		SysPrintf("%x",UART1_Rx_Buf[i]);
	}
	SysPrintf("\r\n");
	if( ((uint16_t)(UART1_Rx_Buf[UART1_Rx_cnt-1]<<8) + (uint16_t)(UART1_Rx_Buf[UART1_Rx_cnt-2])) == CH9120_Dest_PORT) 
	{
		SysPrintf("CH9120 dest port correct\r\n");
		SysPrintf("eth0 dest port:%d\r\n",CH9120_Dest_PORT);
	}
	else
	{
		SysPrintf("CH9120 dest port err and re-config\r\n");
		CH9120_Init(CH9120_Mode);
		
	}
	
	UART1_Rx_cnt = 0;
	HAL_UART_Transmit(&huart1,CH9120_Read_Baund_cmd,3,1000);
	SysPrintf("CH9120 baund: ");
	HAL_Delay(1000); 
	for(i=0;i<UART1_Rx_cnt;i++)
	{
		SysPrintf("%x",UART1_Rx_Buf[i]);
	}
	SysPrintf("\r\n");
	
	
	UART1_Rx_cnt = 0;
	HAL_UART_Transmit(&huart1,CH9120_Read_Mode_cmd,3,1000);
	SysPrintf("CH9120 mode: ");
	HAL_Delay(1000);
	for(i=0;i<UART1_Rx_cnt;i++)
	{
		if(UART1_Rx_Buf[0]==CH9120_TCP_Server_Mode)
			SysPrintf("CH9120_TCP_Server_Mode\r\n");
		else if(UART1_Rx_Buf[0]==CH9120_TCP_Client_Mode)
			SysPrintf("CH9120_TCP_Client_Mode\r\n");
		else if(UART1_Rx_Buf[0]==CH9120_UDP_Server_Mode)
			SysPrintf("CH9120_UDP_Server_Mode\r\n");
		else if(UART1_Rx_Buf[0]==CH9120_UDP_Client_Mode)
			SysPrintf("CH9120_UDP_Client_Mode\r\n");
		else
			SysPrintf("error\r\n");
	}

	
	UART1_Rx_cnt = 0;
	HAL_UART_Transmit(&huart1,CH9120_Read_sourceIP_cmd,3,1000);
	SysPrintf("CH9120 source ip: ");
	HAL_Delay(1000);
	if(UART1_Rx_cnt>=4)
	{
		SysPrintf("eth0 local ip:%d.%d.%d.%d\r\n",UART1_Rx_Buf[UART1_Rx_cnt-4],UART1_Rx_Buf[UART1_Rx_cnt-3],UART1_Rx_Buf[UART1_Rx_cnt-2],UART1_Rx_Buf[UART1_Rx_cnt-1]);
	}
	SysPrintf("\r\n");
	
	
	CH9120_Config_OFF();
}

void CH9120_Get_Local_Info(void)
{
	uint8_t i;
	uint8_t j = 6;
	uint8_t CH9120_Get_Local_ip_cmd[3+1]={0x57,0xab,0x61};
	uint8_t CH9120_Get_Local_mask_cmd[3+1]={0x57,0xab,0x62};
	uint8_t CH9120_Get_Local_gw_cmd[3+1]={0x57,0xab,0x63};
	
	
	UART1_Rx_cnt = 0;
	SysPrintf("get CH9120 local info begin\r\n");
	HAL_UART_Transmit(&huart1,CH9120_Get_Local_ip_cmd,3,1000);
	HAL_Delay(250);	
	if(UART1_Rx_cnt>=4)
	{
		SysPrintf("eth0 local ip:%d.%d.%d.%d\r\n",UART1_Rx_Buf[0],UART1_Rx_Buf[1],UART1_Rx_Buf[2],UART1_Rx_Buf[3]);
	}
	
	UART1_Rx_cnt = 0;
	HAL_UART_Transmit(&huart1,CH9120_Get_Local_mask_cmd,3,1000);
	HAL_Delay(250);	
	if(UART1_Rx_cnt>=4)
	{
		SysPrintf("eth0 local mask:%d.%d.%d.%d\r\n",UART1_Rx_Buf[0],UART1_Rx_Buf[1],UART1_Rx_Buf[2],UART1_Rx_Buf[3]);
	}
	
	UART1_Rx_cnt = 0;
	HAL_UART_Transmit(&huart1,CH9120_Get_Local_gw_cmd,3,1000);
	HAL_Delay(250);	
	if(UART1_Rx_cnt>=4)
	{
		SysPrintf("eth0 local gw:%d.%d.%d.%d\r\n",UART1_Rx_Buf[0],UART1_Rx_Buf[1],UART1_Rx_Buf[2],UART1_Rx_Buf[3]);
	}
}


void CH9120_Init(uint8_t mode_val)
{
	CH9120_RESET();
	
	




	
	
	CH9120_Config_ON();
	
	CH9120_Link_ClearData();
	CH9120_Set_Mode(mode_val);
	CH9120_Set_Dest_IP();
	CH9120_Set_Dest_PORT();
	CH9120_Updata_EEPROM();
	CH9120_Active_Config();
	CH9120_Get_Local_Info();
	
	CH9120_Config_OFF();
}

